<template>
  <div class="home_box">
    <div class="background">
      <img src="../../img/lake.png" style="width: 100%; height: 100%" alt="" />
    </div>
    <div height="80px" class="header">
      <div class="title">
        <div style="padding-left: 10px; float: right">吾将上下而求索</div>
        <img
          src="../../img/qiusuo.png"
          alt=""
          style="height: 46px; padding-top: 7px; float: left"
        />
      </div>
      <div class="user">
        你好, &nbsp;{{ this.username
        }}<el-dropdown @command="handleCommand">
          <span class="el-dropdown-link"
            ><i class="el-icon-arrow-down el-icon--right"></i
          ></span>
          <el-dropdown-menu slot="dropdown">
            <el-dropdown-item command="publish">我的发布</el-dropdown-item>
            <el-dropdown-item command="verify">验证邮箱</el-dropdown-item>
            <el-dropdown-item command="logout">退出登录</el-dropdown-item>
          </el-dropdown-menu>
        </el-dropdown>
      </div>
    </div>
    <el-container class="mid_layout">
      <el-aside
        width="200px"
        style="background-color: rgba(255, 255, 255, 0.4)"
      >
        <div style="margin-bottom: 30px; margin-top: 30px">
          <el-button
            type="primary"
            icon="el-icon-circle-plus-outline"
            class="create-btn"
            @click="create"
            >创建问卷</el-button
          >
        </div>
        <div class="card" @click="publish">已发布的问卷</div>
        <div class="card" @click="bin">回收站</div>
        <!-- <div class="card" @click="message">消息</div> -->
      </el-aside>
      <el-main style="overflow-y: auto"><router-view></router-view></el-main>
    </el-container>
    <el-dialog title="选择要创建的问卷类型" :visible.sync="isCreate">
      <div style="text-align: center">
        <el-button @click="create_normal">普通问卷</el-button>
        <!-- <el-button @click="create_vote">投票问卷</el-button>
        <el-button @click="create_signup">报名问卷</el-button> -->
        <el-button @click="create_exam">考试问卷</el-button>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button @click="isCreate = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      isCreate: false,
      username: "",
    };
  },
  created() {
    this.$store.commit("initLog");
    if (this.$store.state.isLogin === false) this.$router.push("/login");
    this.username = sessionStorage.getItem("username");
  },
  methods: {
    getUsername() {
      return window.sessionStorage.getItem("username");
    },
    publish() {
      this.$router.push("/publish");
    },
    bin() {
      this.$router.push("/bin");
    },
    create() {
      this.isCreate = true;
    },
    logout() {
      this.$http
        .post("/logout")
        .then((res) => {
          this.$router.push("/login");
          window.sessionStorage.clear();
          this.$store.commit("logout");
          this.$message("用户退出!");
        })
        .catch((err) => {
          if (err.response.status == 401) {
            this.$message.error("用户未登录!");
          } else if (err.response.status == 500) {
            this.$message.error("服务器错误,请稍后再试!");
          } else {
            this.$message.error("未知错误,请稍后再试!");
          }
        });
    },
    verify() {
      this.$http
        .put("/verify/mail/send")
        .then((res) => {
          this.$message("邮件已发送,请前往邮箱进行验证!");
        })
        .catch((err) => {
          if (err.response.status === 401) {
            this.$message.error("用户未登录");
            this.$router.push("/login");
          } else if (err.response.status === 403) {
            this.$message.success("邮箱已完成验证!");
          } else {
            this.$message.error("网络错误,请稍后再试!");
          }
        });
    },
    handleCommand(command) {
      if (command === "logout") this.logout();
      if (command === "verify") this.verify();
      if (command === "mypublish") this.mypublish();
    },
    create_normal() {
      this.$router.push("/create_normal");
    },
    create_exam() {
      this.$router.push("/create_exam");
    },
    create_vote() {
      alert("敬请期待...");
    },
    create_signup() {
      alert("敬请期待...");
    },
  },
};
</script>


<style lang="less" scoped>
.home_box {
  height: 100%;
  width: 100%;
  min-width: 1600px;
}
.background {
  // background-image: url("../../img/1.jpg");
  /* 相对于视口固定 */
  // background-attachment: fixed;
  // background-size: cover;
  // background-position: 50% 50%;
  // position: relative;
  // text-align: center;
  // overflow: hidden;
  background-size: cover;
  width: 100%;
  height: 100%;
  position: fixed;
  z-index: -1;
}
.header {
  background-color: rgba(240, 240, 240, 0.8);
  height: 60px;
  position: relative;
}
.mid_layout {
  width: 75%;
  height: 100%;
  margin: 0 auto;
  background-color: rgba(255, 255, 255, 0.6);
}
.title {
  // position: absolute;
  display: block;
  float: left;
  font-size: 34px;
  font-weight: 600;
  font-family: "SimSun";
  line-height: 60px;
  margin-left: 10px;
}
.user {
  display: flex;
  float: right;
  line-height: 60px;
  margin-right: 30px;
}
.create-btn {
  margin-left: 5%;
  width: 90%;
  height: 50px;
}
.card {
  width: 100%;
  height: 60px;
  text-align: center;
  font-size: 18px;
  line-height: 60px;
  user-select: none;
}
.card:hover {
  background-color: rgba(108, 180, 255, 0.5);
}
</style>